package jk.spider.core.storage.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import jk.spider.core.storage.CheckInfoDAO;
import jk.spider.model.CheckInfo;

import org.apache.log4j.Logger;

public class CheckInfoDAOImpl implements CheckInfoDAO {

	protected static final Logger log = Logger.getLogger(CheckInfoDAOImpl.class);
	protected DAOHelp daoHelp;
	public CheckInfoDAOImpl(DAOHelp daoHelp) {
		this.daoHelp = daoHelp;
	}
	
	public void create(CheckInfo info) {
		StringBuffer sql = new StringBuffer();
		sql.append("insert into spider_check (sid, pid, field, url, cType, site, sType, content");
		sql.append(" ) values ( ");
		sql.append(daoHelp.format(info.getSid()) + ",");
		sql.append(daoHelp.format(info.getPid()) + ",");
		sql.append(daoHelp.format(info.getField()) + ",");
		sql.append(daoHelp.format(info.getUrl()) + ",");
		sql.append(daoHelp.format(info.getCType()) + ",");
		sql.append(daoHelp.format(info.getSite()) + ",");
		sql.append(daoHelp.format(info.getSType()) + ",");
		sql.append(daoHelp.format(info.getContent()) + ")");
		daoHelp.execSql(sql.toString());
	}

	public List<CheckInfo> get() {
		List<CheckInfo> list = new ArrayList<CheckInfo>();
		Statement stmt = null;
		String sql = "select * from spider_check where cType <> 5 order by sid ";
		ResultSet rs = null;
		try {
			stmt = daoHelp.getConnection().createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				CheckInfo checkInfo = new CheckInfo();
				checkInfo.setSid(rs.getString(1));
				checkInfo.setPid(rs.getString(2));
				checkInfo.setField(rs.getString(3));
				checkInfo.setUrl(rs.getString(4));
				checkInfo.setCType(rs.getInt(5));
				checkInfo.setSite(rs.getString(6));
				checkInfo.setSType(rs.getString(7));
				checkInfo.setContent(rs.getString(8));
				list.add(checkInfo);
			}
			daoHelp.safeClose(rs);
			daoHelp.safeClose(stmt);
		} catch (SQLException e) {
			log.error("SQLException -> ", e);
			daoHelp.safeClose(rs);
			daoHelp.safeClose(stmt);
		} finally {
			daoHelp.safeClose(rs);
			daoHelp.safeClose(stmt);
		}
		
		return list;
	}
}
